<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="box-card">
        <el-button type="primary" icon="el-icon-plus" @click="dialogVisible = true">新增角色</el-button>
        <el-tabs v-model="activeName">
          <el-tab-pane label="用户管理" name="first">
            <el-table
              :data="tableData"
              style="width: 100%;margin:15px;"
              border
            >
              <el-table-column
                label="序号"
                width="80"
                type="index"
              />
              <el-table-column
                prop="name"
                label="角色名"
                width="180"
                sortable
              />
              <el-table-column
                prop="description"
                label="描述"
                sortable
              />
              <el-table-column
                label="操作"
              >
                <template slot-scope="{row}">
                  <el-button type="text">分配权限</el-button>
                  <el-button type="text" @click="onEdit(row)">修改</el-button>
                  <el-button type="text">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页 -->
            <el-pagination
              :current-page="queryData.page"
              :page-sizes="[2, 5, 10, 20]"
              :page-size="queryData.pagesize"
              layout="total, sizes, prev, pager, next"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </el-tab-pane>

          <el-tab-pane label="公司管理" name="second">
            <el-form ref="form" label-width="80px">
              <el-row>
                <el-col :offset="3" :span="8">
                  <el-form-item label="公司名称">
                    <el-input v-model="company.name" disabled />
                  </el-form-item>
                  <el-form-item label="公司地址">
                    <el-input v-model="company.companyAddress" disabled />
                  </el-form-item>
                  <el-form-item label="公司电话">
                    <el-input v-model="company.companyPhone" disabled />
                  </el-form-item>
                  <el-form-item label="邮箱">
                    <el-input v-model="company.mailbox" disabled />
                  </el-form-item>
                  <el-form-item label="备注">
                    <el-input v-model="company.remarks" disabled />
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 新增 -->
      <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        width="50%"
        @close="addDialogClose"
      >
        <el-form ref="addRoleForm" :model="addRoleForm" :rules="addRoleRules" label-width="100px" class="demo-ruleForm">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="addRoleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="addRoleForm.description" />
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="onSave">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { getCompany } from '@/api/company.js'
import { getRolesList,
  addRoles,
  editRoles
} from '@/api/roles.js'
export default {
  data() {
    return {
      activeName: 'first',
      company: {},
      tableData: [],
      queryData: {
        page: 1,
        pagesize: 2
      },
      total: 0,
      dialogVisible: false,
      addRoleForm: {
        name: '',
        description: ''
      },
      addRoleRules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' },
          { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入角色描述', trigger: 'blur' },
          { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    ...mapGetters(['companyId'])
  },
  created() {
    // console.log(this.companyId)
    this.loadGetCompany()
    this.loadRolesList()
  },

  methods: {
    // 加载公司信息
    async loadGetCompany() {
      try {
        const res = await getCompany(this.companyId)
        // console.log(res)
        this.company = res
      } catch (error) {
        console.log('获取公司信息失败')
        console.log(error)
      }
    },
    // 加载角色信息
    async loadRolesList() {
      try {
        const res = await getRolesList(this.queryData)
        console.log(res)
        this.tableData = res.rows
        this.total = res.total
      } catch (error) {
        console.log(error)
      }
    },
    // 根据页码更新数据
    handleCurrentChange(val) {
      this.queryData.page = val
      this.loadRolesList()
    },
    // 根据每页条数更新数据
    handleSizeChange(val) {
      this.queryData.pagesize = val
      this.loadRolesList()
    },
    // 点击确认
    async onSave() {
      try {
        await this.$refs.addRoleForm.validate()
        // this.dialogVisible = false
        if (this.addRoleForm.id) {
          // 修改
          // console.log('修改')
          await editRoles(this.addRoleForm)
          this.$message.success('修改成功')
        } else {
          // 添加
          await addRoles(this.addRoleForm)
          this.$message.success('添加成功')
        }
        this.dialogVisible = false
        this.loadRolesList()
      } catch (error) {
        console.log(error)
      }
    },
    // 修改
    onEdit(row) {
      this.dialogVisible = true
      this.addRoleForm = { ...row }
    },
    addDialogClose() {
      this.$refs.form.resetFields()
      this.addRoleForm = {
        name: '',
        description: ''
      }
    }

  }
}
</script>

<style scoped lang='scss'>
  ::v-deep .el-tabs__header{
    .el-tabs__item{
      width: 140px;
      text-align: center;
      font-size:18px;
    }
  }
</style>
